package com.server;

import java.io.IOException;
import java.sql.SQLException;

import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.*;
import java.util.Calendar;

public class FlightSearchServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	
	private String ToSqlDate(String date)
	{
		String[] oldString = date.split("/");
		String[] newString = {oldString[2], oldString[0], oldString[1]};
		
		String sqlDate = newString[0] + "-" + newString[1] + "-" + newString[2];
		return sqlDate;
	}
	
	private boolean IsNull(String s)
	{
		if (s.isEmpty() || s == null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private boolean IsLessThanToday(String date)
	{
		Calendar now = Calendar.getInstance();
		int today_year = now.get(Calendar.YEAR);
		int today_month = (now.get(Calendar.MONTH) + 1 );
		int today_day = now.get(Calendar.DATE);	
		
		String[] oldString = date.split("/");
		String[] newString = {oldString[2], oldString[0], oldString[1]};
		
		int year = Integer.parseInt(newString[0]);
		int month = Integer.parseInt(newString[1]);
		int day= Integer.parseInt(newString[2]);
		
		if (year >= today_year && month >= today_month && day >= today_day)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		ArrayList<FlightInfo> flightList = new ArrayList<FlightInfo>();
		ArrayList<ConnectInfo> connectList = new ArrayList<ConnectInfo>();
		
		String username = req.getParameter("username");
		String date = req.getParameter("date");
				
		String leaving = req.getParameter("leaving");
		String arriving = req.getParameter("arriving");
		
		String msg = null;
				
		boolean error = false;
						
		try 
		{			
			FlightHelper.Setup(connection);
						// arriving and leaving have values
			
			if (date.contains("/") == false && date.isEmpty() == false)
			{
				error = true;
			}
			else if (date.isEmpty() == false && leaving.isEmpty() == true && leaving.isEmpty() == true)
			{
				error = true;
				msg = "You cannot make a search with just the date.";
			}
			else
			{	
				if (IsNull(leaving) == true && IsNull(arriving) == true)
				{			
					if (date == null || date.isEmpty())
					{
						flightList = FlightHelper.GetAllFlights();
					}
					else
					{
						flightList = FlightHelper.GetAllFlights(date);
					}
				}
				else if (IsNull(leaving) == false && IsNull(arriving) == false)
				{		
					if (date == null || date.isEmpty())
					{
						flightList = FlightHelper.GetFlightsByLeavingArriving(leaving, arriving);
						connectList = FlightHelper.GetConnectingFlight(leaving, arriving);
					}
					else
					{
						date = ToSqlDate(date);
						flightList = FlightHelper.GetFlightsByLeavingArriving(leaving, arriving, date);
						connectList = FlightHelper.GetConnectingFlight(leaving, arriving, date);
					}
				}
				else 
				{
					error = true;
					msg = "You need to specify both departure and arrival locations.";
					
				}
			}
									
			if (error == false)
			{	
				boolean isEmpty1 = false;
				boolean isEmpty2 = false;
				
				req.setAttribute("username", username);
				req.setAttribute("msg", msg);
				
				if (flightList.size() > 0)
				{
					isEmpty1 = false;
				}
				else 
				{
					isEmpty1 = true;
				}
				
				if (connectList.size() > 0)
				{
					isEmpty2 = false;
				}
				else
				{
					isEmpty2 = true;
				}
				
				req.setAttribute("isEmpty1", isEmpty1);
				req.setAttribute("isEmpty2", isEmpty2);
				req.setAttribute("flightList", flightList);
				req.setAttribute("connectList", connectList);
				req.getRequestDispatcher("flight/search_flight_result.jsp").forward(req, resp);
			}
			else
			{				
				
					
				req.setAttribute("username", username);
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("flight/search_error.jsp").forward(req, resp);
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}		
				
	}
}



